package middlewares

import (
	"strings"

	"github.com/kataras/iris"

	"im/app"
	"im/app/controller"
	"im/errcode"
	"im/service"
)

// app token验证
func AppsCheck(ctx iris.Context) {
	var base = controller.Base{Ctx: ctx}
	appToken := strings.Trim(ctx.URLParam(app.GET_NAME_OF_APP_TOKEN), " ")
	if appToken == "" {
		base.SendCry(errcode.NewError(errcode.UnauthorizedOperation))
		return
	}

	appId, err := service.NewAppService().GetToken(appToken)
	if err != nil {
		base.SendCry(errcode.NewError(errcode.UnauthorizedOperation))
		return
	}
	ctx.Values().Set(app.MIDDLEWARE_APP_ID_KEY, appId)
	ctx.Next()
}
